package com.borazone.extra.launcher;

import java.io.File;
import java.text.MessageFormat;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMRunner;

public class MHPLocalApplicationLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate implements
		ILaunchConfigurationDelegate2 {

	public void launch(ILaunchConfiguration configuration, String mode,
			ILaunch launch, IProgressMonitor monitor) throws CoreException {
		
		if( monitor == null ) {
			monitor = new NullProgressMonitor();
		}
		monitor.beginTask(MessageFormat.format("{0}...", new String[]{configuration.getName()}), 3); //$NON-NLS-1$

		if( monitor.isCanceled() ) {
			return;
		}
		
		try {
			monitor.subTask( LaunchingMessages.JavaLocalApplicationLaunchConfigurationDelegate_Verifying_launch_attributes____1 );

			String mainTypeName = verifyMainTypeName(configuration);
			
			IVMInstall vm = verifyVMInstall(configuration);
			IVMRunner runner = vm.getVMRunner(mode);
			
			File workingDir = verifyWorkingDirectory(configuration);
			String workingDirName = null;
			if( workingDir != null ) {
				workingDirName = workingDir.getAbsolutePath();
			}
			
			// Environment variables
			String[] envp = getEnvironment(configuration);
			
			runner.run( null, null, null );
			
		} finally {
			monitor.done();
		}
	}

	
}
